package com.un.ebs.product.aspect;


import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.un.ebs.contract.domain.ContractItemExt;
import com.un.ebs.contract.service.ContractService;
import com.un.ebs.core.util.AESTool;
import com.un.ebs.inventory.domain.EntRelationExt;
import com.un.ebs.inventory.service.BusinessRelationService;
import com.un.ebs.product.domain.ProductSku;
import com.un.ebs.product.service.impl.ProductSkuServiceImpl;
import com.un.ebs.sys.domain.UserExt;
import com.un.ebs.sys.service.UserService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Aspect  // 使用@Aspect注解声明一个切面
@Component
public class UpperLowerShelfAspect {

//    private static final String notice_url = "http://localhost:8080/recharge-order/test";

    private static final String UPPER = "true";

    private static final String LOWER = "false";
    @Autowired
    private ProductSkuServiceImpl productSkuServiceImpl;
    @Autowired
    private BusinessRelationService businessRelationService;
    @Autowired
    private ContractService contractService;
    @Autowired
    private UserService userService;

    @Pointcut("@annotation(Upper)")
    public void upper() {
    }

    @Pointcut("@annotation(Lower)")
    public void lower() {
    }

    @After(value = "upper()")
    public void upperAfterMethod(JoinPoint joinPoint) {
        sendMessage(joinPoint, UPPER);
    }

    @After(value = "lower()")
    public void lowerAfterMethod(JoinPoint joinPoint) {
        sendMessage(joinPoint, LOWER);
    }


    public void sendMessage(JoinPoint joinPoint, String status) {
        Object[] args = joinPoint.getArgs();
        String id = ((Map<String, String>) args[0]).get("id");
        if (StrUtil.isNotEmpty(id)) {
            JSONObject jsonObject = new JSONObject();
            ProductSku productSku = productSkuServiceImpl.getById(id);
            String skuCode = productSku.getSkuCode();
            jsonObject.put("skuCode", skuCode);
            jsonObject.put("is_sale", status);
            List<ContractItemExt> contractItemExts = contractService.querySalePriceBySkuCode(productSku.getSkuCode());
            for (ContractItemExt item : contractItemExts) {
                if (item.getBusinessEntName().contains("中国银联")) {
                    EntRelationExt ent = businessRelationService.queryCustomerById(item.getBusinessEntId());
                    if (ent == null) {
                        continue;
                    }
                    String openApiUserId = ent.getOpenApiUserId();
                    if (StrUtil.isEmpty(openApiUserId)) {
                        continue;
                    }
                    UserExt userExt = userService.getUserExt(openApiUserId);
                    if (userExt == null) {
                        continue;
                    }
                    String password = userExt.getPassword();
                    if (StrUtil.isEmpty(password)) {
                        continue;
                    }
                    String security_key = AESTool.Decrypt(password);
                    if (StrUtil.isEmpty(security_key)) {
                        continue;
                    }
//                    String toSignatureChars = SignatureUtil.generateSignStr(jsonObject, security_key);
//                    String sign = "";
//                    try {
//                        sign = SignatureUtil.messageDigestEncrypt(toSignatureChars, RechargeHelper.SIGNATURE_TYPE_SHA1);
//                    } catch (Exception e) {
//                        e.printStackTrace();
//                    }
//                    jsonObject.put(RechargeHelper.SIGNATURE, sign);
//                    RechargeHelper.toThreeTimesCustomer(notice_url, jsonObject);
                }
            }
            //IPage<EntRelationDto> query = businessRelationService.query(null, BusinessRelationTypeEnum.SUPPLIER, 10000, 1);
//            List<EntRelationDto> records = query.getRecords();
//            for (EntRelationDto record : records) {
//                if (record.getCountyName().contains("中国银联")) {
//
//                    ContractItemExt salePrice = contractService.querySalePriceBySkuCode(record.getEntId(), productSku.getSkuCode());
//                    EntRelationExt ent = businessRelationService.queryCustomerById(salePrice.getBusinessEntId());
//                    ent.getOpenApiUserId();
//
//                }
//            }
        }
    }
/*
    @After
    public Object around(ProceedingJoinPoint point) throws Throwable {
        return null;
    }*/

}
